package com.yilian.xlmusic.android;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.yilian.xlmusic.android.base.MyBaseActivity;
import com.yilian.xlmusic.android.databinding.ActivityMoreMusicBinding;
import com.yilian.xlmusic.android.utils.TryLinearLayoutManager;
import com.yilian.xlmusic.android.utils.net.AppConstants;
import com.yilian.xlmusic.android.view.adapter.ItemRecycleViewAdapter;
import com.yilian.xlmusic.android.view.bean.GetCollectionBean;
import com.yilian.xlmusic.android.view.bean.MusicPlayerBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import rxhttp.RxHttp;

public class MoreMusicActivity extends MyBaseActivity {
    private int PAGE = 0;
    private int PER_PAGE = 10;
    private List<MusicPlayerBean.DataBean> listBeans = new ArrayList<>();
    private ItemRecycleViewAdapter itemRecycleViewAdapter2;
    ActivityMoreMusicBinding binding;
    @Override
    public int getNetType() {
        return 0;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_more_music;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //更新适配器的内容
        if (AppConstants.changeItems.size() > 0) {
            AppConstants.refreshView(AppConstants.changeItems.get(0));
        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        if(XlMusicActivity.mMediaPlayerUtils.isPlaying()){
            XlMusicActivity.mMediaPlayerUtils.stop();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(XlMusicActivity.mMediaPlayerUtils.isPlaying()){
            XlMusicActivity.mMediaPlayerUtils.stop();
        }
    }

    @Override
    public void initView(Bundle var1) {
        setTitleView();
        binding = DataBindingUtil.setContentView(this,R.layout.activity_more_music);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.refreshLayout.setEnableRefresh(false);
        binding.refreshLayout.autoLoadMore();
        binding.refreshLayout.setEnableScrollContentWhenRefreshed(true);//是否在刷新完成时滚动列表显示新的内容 1.0.5
        binding.refreshLayout.setEnableLoadMoreWhenContentNotFull(false);//是否在列表不满一页时候开启上拉加载功能
        binding.recyclerView.setLayoutManager(new TryLinearLayoutManager(this));

        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                PAGE++;
                getCategoryResource();

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });
        showDialog();
        binding.backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getCategoryResource();

    }

    private void setTitleView() {
        hideTitleBar();
        setIsFullScreen(true, true, R.color.color_000000);
        setStatusBarTextColor2(Color.parseColor("#000000"));
        setBackEnable(false);
    }

    private void getCategoryResource() {
        AppConstants.METHOD_NAME = "推荐歌单：";
        RxHttp.get("/ylmusic-gateway/collection")
                .addHeader(AppConstants.ACCESS_TOKEN_KEY, AppConstants.ACCESS_TOKEN)
                .add("page", PAGE)
                .add("per_page", PER_PAGE)
                .add("category_id", getIntent().getIntExtra("category_id", 0))//category_id在0表示我的收藏，其他为音乐分类ID
                .asClass(GetCollectionBean.class)
                .observeOn(AndroidSchedulers.mainThread())  //主线程回调观察者
                .subscribe(callBackBean -> {
                    binding.actionTitleTv.setText(callBackBean.getData().getTitle());
                    listBeans.addAll(callBackBean.getData().getList());
                    if (listBeans.size() > 0 && callBackBean.getData().getList().size() == 0) {//下一页没有数据了
                        binding.refreshLayout.setEnableLoadMore(false);
                        if (listBeans.size()>PER_PAGE){
                            View inflate = View.inflate(MoreMusicActivity.this, R.layout.none, null);
                            itemRecycleViewAdapter2.addFooterView(inflate);
                        }

                    }

                    if (itemRecycleViewAdapter2 != null) {
                        //如果有音乐正在播放，就刷新下View
                        if (XlMusicActivity.mMediaPlayerUtils.isPlaying()) {
                            if (AppConstants.changeItems.size() > 0) {
                                //更新下正在播放歌曲的状态
                                //   AppConstants.changeItems.get(0).getDataBean().setIs_play(true);
                                //  itemRecycleViewAdapter2.getData().set(AppConstants.changeItems.get(0).getChangePosition(), AppConstants.changeItems.get(0).getDataBean());
                                // itemRecycleViewAdapter2.notifyItemChanged(AppConstants.changeItems.get(0).getChangePosition(), AppConstants.changeItems.get(0).getDataBean());
                                listBeans.set(AppConstants.changeItems.get(0).getChangePosition(), AppConstants.changeItems.get(0).getDataBean());
                            }
                            itemRecycleViewAdapter2.setList(listBeans);

                        }
                        itemRecycleViewAdapter2.notifyDataSetChanged();
                    } else {
                        itemRecycleViewAdapter2 = new ItemRecycleViewAdapter(R.layout.music_player_item2, listBeans, true);
                        binding.recyclerView.setAdapter(itemRecycleViewAdapter2);

                    }
                    if (isShowDialog()){
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                closeDialog();
                                binding.refreshLayout.setVisibility(View.VISIBLE);
                            }
                        },500);

                    }
                    binding.refreshLayout.finishLoadMore();

                }, throwable -> {
                    Log.e("请求失败了", "请求失败了" + throwable);
                });
    }
}